package web.beans;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import entity.Car.bo.CarBo;
import entity.Car.model.Car;
import entity.Car.model.reference.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import util.CreatePDF;

import javax.annotation.PostConstruct;
import javax.faces.model.ArrayDataModel;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by vanches on 9.1.15.
 */
public class CarBean {
    private CarBo carBo;
    private List<Car> cars;
    private List<BodyType> bodyTypes;
    private List<Brand> brands;
    private List<Color> colors;
    private List<Condition> conditions;
    private List<Currency> currencies;
    private List<Model> models;
    private List<Privod> privods;
    private List<Transmission> transmissions;
    private List<TypeEngine> typeEngines;
    private boolean sortPriceAscending = true;
    private boolean sortIdAscending=true;
    private boolean sortByDateAscending=true;
    private CreatePDF createPDF;

    @PostConstruct
    public void init(){
        cars=carBo.getAllCar();
        bodyTypes=carBo.getAllBodyType();
        brands=carBo.getAllBrand();
        colors=carBo.getAllColor();
        conditions=carBo.getAllCondition();
        currencies=carBo.getAllCurrency();
        models=carBo.getAllModel();
        privods=carBo.getAllPrivod();
        transmissions=carBo.getAllTransmission();
        typeEngines=carBo.getAllTypeEngine();

    }

    public void exportPDF(){
        createPDF.exportListCar(cars);
    }

    public String sortByPrice() {

        if(sortPriceAscending){

            //ascending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return Double.compare(o1.getTransport().getPrice(), o2.getTransport().getPrice());

                }

            });
            sortPriceAscending = false;

        }else{

            //descending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return Double.compare(o2.getTransport().getPrice(), o1.getTransport().getPrice());

                }

            });
            sortPriceAscending = true;
        }

        return null;
    }
    public String sortById() {

        if(sortIdAscending){

            //ascending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return Integer.compare(o1.getId(), o2.getId());

                }

            });
            sortIdAscending = false;

        }else{

            //descending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return Integer.compare(o1.getId(), o2.getId());

                }

            });
            sortIdAscending = true;
        }

        return null;
    }
    public String sortByDate() {

        if(sortByDateAscending){

            //ascending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return o1.getTransport().getDate().compareTo(o2.getTransport().getDate());
                }

            });
            sortByDateAscending = false;

        }else{

            //descending order
            Collections.sort(cars, new Comparator<Car>() {

                @Override
                public int compare(Car o1, Car o2) {

                    return o2.getTransport().getDate().compareTo(o1.getTransport().getDate());

                }

            });
            sortByDateAscending = true;
        }

        return null;
    }

    public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        for(int i=0; i < header.getPhysicalNumberOfCells();i++) {
            HSSFCell cell = header.getCell(i);

            cell.setCellStyle(cellStyle);
        }
    }

    public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
        Document pdf = (Document) document;
        pdf.open();
        pdf.setPageSize(PageSize.A4);

    }

    //region GET and SET

    public CarBo getCarBo() {
        return carBo;
    }

    public void setCarBo(CarBo carBo) {
        this.carBo = carBo;
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    public List<BodyType> getBodyTypes() {
        return bodyTypes;
    }

    public void setBodyTypes(List<BodyType> bodyTypes) {
        this.bodyTypes = bodyTypes;
    }

    public List<Brand> getBrands() {
        return brands;
    }

    public void setBrands(List<Brand> brands) {
        this.brands = brands;
    }

    public List<Color> getColors() {
        return colors;
    }

    public void setColors(List<Color> colors) {
        this.colors = colors;
    }

    public List<Condition> getConditions() {
        return conditions;
    }

    public void setConditions(List<Condition> conditions) {
        this.conditions = conditions;
    }

    public List<Currency> getCurrencies() {
        return currencies;
    }

    public void setCurrencies(List<Currency> currencies) {
        this.currencies = currencies;
    }

    public List<Model> getModels() {
        return models;
    }

    public void setModels(List<Model> models) {
        this.models = models;
    }

    public List<Privod> getPrivods() {
        return privods;
    }

    public void setPrivods(List<Privod> privods) {
        this.privods = privods;
    }

    public List<Transmission> getTransmissions() {
        return transmissions;
    }

    public void setTransmissions(List<Transmission> transmissions) {
        this.transmissions = transmissions;
    }

    public List<TypeEngine> getTypeEngines() {
        return typeEngines;
    }

    public void setTypeEngines(List<TypeEngine> typeEngines) {
        this.typeEngines = typeEngines;
    }

    public CreatePDF getCreatePDF() {
        return createPDF;
    }

    public void setCreatePDF(CreatePDF createPDF) {
        this.createPDF = createPDF;
    }

    //endregion
}
